package Visao;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.text.MaskFormatter;

import java.awt.Color;
import javax.swing.JMenuBar;
import javax.swing.JMenu;
import javax.swing.JLabel;
import javax.swing.JOptionPane;

import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import javax.swing.JComboBox;

import Controle.VisitanteControle;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.text.ParseException;

import javax.swing.JFormattedTextField;
import javax.swing.ImageIcon;

public class Visitante extends JFrame {

	private JPanel contentPane;
	private JTextField textFieldNome;
	private JTextField textFieldMotivo;
	private JFormattedTextField textFieldCpf;
	private JButton btnCadastrar;
	private JButton btnListar;
	private Modelo.Visitante visitante;
	private VisitanteControle controleVisitante;
	private ControleDeTelas telas;
	private JComboBox comboBox;

	/**
	 * Launch the application.
	 */
	
	/**
	 * Create the frame.
	 * @throws ParseException 
	 */
	public Visitante() throws ParseException {
		telas = new ControleDeTelas();
		controleVisitante = new VisitanteControle();
		setTitle("Cadastro de Visitante");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 451, 300);
		setLocationRelativeTo(null);
		
		JMenuBar menuBar = new JMenuBar();
		setJMenuBar(menuBar);
		
		JMenu mnInicio = new JMenu("Inicio");
		mnInicio.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				telas.getInicio().setVisible(true);
				dispose();
			}
		});
		menuBar.add(mnInicio);
		
		JMenu mnNewMenu = new JMenu("Cadastro Aluno\r\n");
		mnNewMenu.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				telas.getCadastros().setVisible(true);
				dispose();
			}
		});
		menuBar.add(mnNewMenu);
		
		JMenu mnCadastroFuncionario = new JMenu("Cadastro Funcionario");
		mnCadastroFuncionario.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				telas.getCadastroFuncionario().setVisible(true);
				dispose();
			}
		});
		menuBar.add(mnCadastroFuncionario);
		
		JMenu mnBuscas = new JMenu("Buscas");
		mnBuscas.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				telas.getBusca().setVisible(true);
				dispose();
			}
		});
		menuBar.add(mnBuscas);
		
		JMenu mnVisitante = new JMenu("Visitante");
		menuBar.add(mnVisitante);
		contentPane = new JPanel();
		contentPane.setBackground(Color.WHITE);
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		MaskFormatter mkf = new MaskFormatter("***.***.***-**");
		
		JPanel panel = new JPanel();
		panel.setBackground(new Color(0, 139, 139, 50));
		panel.setBounds(43, 11, 350, 167);
		contentPane.add(panel);
		panel.setLayout(null);
		
		JLabel lblCadastrar = new JLabel("Cadastrar");
		lblCadastrar.setBounds(10, 11, 72, 14);
		panel.add(lblCadastrar);
		lblCadastrar.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
		
		JLabel lblNome = new JLabel("Nome");
		lblNome.setBounds(10, 36, 46, 14);
		panel.add(lblNome);
		
		textFieldNome = new JTextField();
		textFieldNome.setBounds(51, 33, 237, 20);
		panel.add(textFieldNome);
		textFieldNome.setColumns(10);
		
		JLabel lblMotivo = new JLabel("Motivo");
		lblMotivo.setBounds(10, 61, 46, 14);
		panel.add(lblMotivo);
		
		textFieldMotivo = new JTextField();
		textFieldMotivo.setBounds(51, 58, 237, 20);
		panel.add(textFieldMotivo);
		textFieldMotivo.setColumns(10);
		
		JLabel lblCpf = new JLabel("Cpf");
		lblCpf.setBounds(10, 86, 46, 14);
		panel.add(lblCpf);
		
		
		
		textFieldCpf = new JFormattedTextField(mkf);
		textFieldCpf.setBounds(51, 83, 237, 20);
		panel.add(textFieldCpf);
		textFieldCpf.setColumns(10);
		
		btnCadastrar = new JButton("Cadastrar");
		btnCadastrar.setBounds(197, 122, 91, 23);
		panel.add(btnCadastrar);
		btnCadastrar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				visitante = new Modelo.Visitante();
				visitante.setNome(textFieldNome.getText());
				visitante.setMotivo(textFieldMotivo.getText());
				visitante.setDocumento(textFieldCpf.getText());
				
				try {
					controleVisitante.salvar(visitante);
					JOptionPane.showMessageDialog(null, "Visitante salvo!");
					textFieldCpf.setText("");
					textFieldMotivo.setText("");
					textFieldNome.setText("");
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		btnCadastrar.setBackground(Color.LIGHT_GRAY);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBackground(new Color(0, 139, 139, 50));
		panel_1.setBounds(43, 189, 350, 64);
		contentPane.add(panel_1);
		panel_1.setLayout(null);
		
		comboBox = new JComboBox();
		comboBox.setBounds(10, 36, 237, 22);
		panel_1.add(comboBox);
		
		JLabel lblListar = new JLabel("Listar");
		lblListar.setBounds(10, 11, 46, 14);
		panel_1.add(lblListar);
		lblListar.setFont(new Font("Tahoma", Font.BOLD | Font.ITALIC, 11));
		
		btnListar = new JButton("Listar");
		btnListar.setBounds(249, 36, 91, 23);
		panel_1.add(btnListar);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setIcon(new ImageIcon("H:\\workspace\\SupportWatchful\\Imagens\\i=magem lois.jpg"));
		lblNewLabel.setBounds(0, 0, 443, 253);
		contentPane.add(lblNewLabel);
		btnListar.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				comboBox.removeAllItems();
				 for(Modelo.Visitante visitante : controleVisitante.listar()){
					 comboBox.addItem(visitante);
				 }
				
			}
		});
	}
}
